package com.chuangke.file.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.common.http.HttpResult;
import com.chuangke.common.utils.SecurityUtils;
import com.chuangke.file.domain.PermissionSaveDTO;
import com.chuangke.file.model.SysFilePermission;
import com.chuangke.file.service.SysFilePermissionService;

/**
 * 文件权限管理Controller
 * 
 * @author chuangke
 * @date "2020-09-03"
 */
@RestController
@RequestMapping("/file/permission")
public class SysFilePermissionController {
    
    @Autowired
    private SysFilePermissionService sysFilePermissionService;

    /**
     * 查询文件权限管理列表
     */
    @PreAuthorize("hasAuthority('login:action')")
    @GetMapping("/list")
    public HttpResult list() {
    	String userId = SecurityUtils.getUserId() ;
        List<SysFilePermission> list = sysFilePermissionService.list(SysFilePermission.AUTH_FILE_TYPE_DBANK,userId);
        return HttpResult.ok(list);
    }
    
    @PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/file")
	public HttpResult list(@RequestParam String fileId) {
		return HttpResult.ok(sysFilePermissionService.listFilePermission(fileId));
	}

    /**
     * 新增文件权限管理
     */
    @PreAuthorize("hasAuthority('login:action')")
    @PostMapping(value = "/save")
    public HttpResult save(@RequestBody PermissionSaveDTO psDto) {
    	sysFilePermissionService.savePermission(psDto);
        return HttpResult.ok();
    }

    /**
     * 删除文件权限管理
     */
    @PreAuthorize("hasAuthority('login:action')")
	@DeleteMapping("/{ids}")
    public HttpResult remove(@PathVariable List<String> ids) {
    	sysFilePermissionService.delete(ids);
        return HttpResult.ok();
    }
}
